//https://leetcode.cn/problems/reverse-string-ii/
package codeRandomThoughts.Test541反转字符串II;

//二刷
public class Solution3 {
    public String reverseStr(String s, int k) {
        char[] chars = s.toCharArray();
        int i = 0;
        while (i < chars.length) {
            int end = Math.min(chars.length - 1, i + k - 1);
            swapStr(chars, i, end);
            i += 2 * k;
        }

        return new String(chars);
    }

    //左闭右闭
    public void swapStr(char[] chars, int begin, int end) {
        while (begin < end) {
            chars[begin] ^= chars[end];
            chars[end] ^= chars[begin];
            chars[begin] ^= chars[end];
            begin++;
            end--;
        }
    }
}
